package com.ruoyi.gis.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.minio.MinIoClientConfig;
import com.ruoyi.framework.minio.MinioFile;
import com.ruoyi.framework.minio.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/gis/icon")
public class GisIconFileUploadController {
    @Autowired
    private MinioService minioService;
    @Autowired
    private MinIoClientConfig minIoClientConfig;

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            String bucket = "gisicon";
            String fileName = file.getOriginalFilename();
            String objectName = IdUtil.getSnowflakeNextIdStr()+"."+FileUtils.getExt(fileName);
            MinioFile minioFile = minioService.uploadFile(file.getInputStream(), bucket, objectName);
            JSONObject data = new JSONObject();
            data.put("url", minIoClientConfig.getFileUrl(bucket,objectName));
            data.put("bucket", bucket);
            data.put("fileName", fileName);
            data.put("objectName", objectName);
            data.put("fileSize", file.getSize());
            return AjaxResult.success(data);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}
